<?php

namespace app\store\controller\shop;

use app\store\controller\Controller;
use app\store\model\store\Shop as ShopModel;
use app\store\model\store\shop\Order as OrderModel;
use think\Db;

/**
 * 订单核销记录
 * Class Order
 * @package app\store\controller\shop
 */
class Order extends Controller {
    /**
     * 订单核销记录列表
     * @param int $shop_id
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($shop_id = 0, $search = '') {
        // 核销记录列表
        $model = new OrderModel;
        $list  = $model->getList($shop_id, $search);
        // 门店列表
        $shopList = (new ShopModel)->getList();
        return $this->fetch('index', compact('list', 'shopList'));
    }
    public function yuyue($shop_id = 0, $search = '') {
        $data           = $this->request->param();
        $where['sy.id'] = ['>', 0];
        if (isset($data['search']) && !empty($data['search'])) {
            $where['sy.real_name'] = ['like', '%' . $data['search'] . '%'];
        }
        if (isset($data['shop_id']) && $data['shop_id'] > 0) {
            $where['sy.store_id'] = ['=', $data['shop_id']];
        }
        $list = Db::name('user_store_yuyue')->alias('sy')
            ->join('user u', 'u.user_id=sy.user_id')
            ->join('store_shop s', 's.shop_id=sy.store_id')
            ->field("u.nickName,s.shop_name,sy.*,FROM_UNIXTIME(sy.create_time) as create_time,FROM_UNIXTIME(sy.yuyue_date,'%Y-%m-%d') as yuyue_date")
            ->where($where)
            ->order('sy.id desc')
            ->paginate(15, false, [
                'query' => \request()->request(),
            ]);
        // 门店列表
        $shopList = (new ShopModel)->getList();
        return $this->fetch('yuyue', compact('list', 'shopList'));
    }
    /**
     * 编辑
     * @param $clerk_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function yuyue_edit($id) {

        if (!$this->request->isAjax()) {
            $model = Db::name('user_store_yuyue')->alias('sy')
                ->join('user u', 'u.user_id=sy.user_id')
                ->join('store_shop s', 's.shop_id=sy.store_id')
                ->field("sy.status,u.nickName,s.shop_name,sy.*,FROM_UNIXTIME(sy.create_time) as create_time,FROM_UNIXTIME(sy.yuyue_date,'%Y-%m-%d') as date,yuyue_date")
                ->where('sy.id', $id)
                ->find();
            if ($model['status'] == 0) {
                $model['status'] = 1;
            }
            if ($model['yuyue_date'] == 0) {
                $model['yuyue_date'] = date('Y-m-d');
            } else {
                $model['yuyue_date'] = $model['date'];
            }

            return $this->fetch('yuyue_edit', compact('model'));
        }
        $data = $this->postData('clerk');

        $update_data['yuyue_date'] = strtotime($data['yuyue_date']);
        $update_data['yuyue_time'] = $data['yuyue_time'];
        $update_data['status']     = $data['status'];

        Db::name('user_store_yuyue')->where('id', $data['id'])
            ->update($update_data);
        return $this->renderSuccess('更新成功', url('shop.order/yuyue'));

    }

}